import {Effect,Reducer,Subscription} from 'umi';
import {login,getUserInfo,getViewAuthority} from '@/services/user';
import {setCookie,getCookie} from '@/utiles/Cookie';
import {ViewData} from '@/interface/user.d';
import menuList from '@/config/userViewAuthority'
let  isUserInfo:boolean = false;

export interface UserModelState{
    isLogin:boolean,
    userInfo:{[propName:string]:any},
    userMenu:ViewData[]
}

export interface UserModelType{
    namespace:'login',
    state:UserModelState,
    effects:{
        Login:Effect,
        eqit:Effect,
        getUserInfo:Effect,
        getViewAuthority:Effect
    },
    reducers:{
        save:Reducer<UserModelState>
    },
    subscriptions: { setup: Subscription };
}

const UserModel:UserModelType = {
    namespace:'login',
    state:{
        isLogin:false,
        userInfo:{},
        userMenu:[]
    },
    effects:{
        *Login({payload},{call,put}){

            let result = yield call(login,payload);
            if(result.code === 1){
                setCookie('token',result.token);
                yield put({
                    type:'save',
                    payload:{isLogin:true}
                })
                yield put({
                    type: 'getViewAuthority'
                })
            }
        },
        *eqit({payload},{call,put}){
            yield put({
                type:'save',
                payload:{isLogin:false}
            })
        },
        *getUserInfo({payload},{call,put}){
            let data = yield call(getUserInfo);
            
            if(data.code === 1){
                isUserInfo = true;
                yield put({ 
                    type:'save',
                    payload:{userInfo:data.data}
                })
                yield put({
                    type:'getViewAuthority',
                    payload:data.data
                })
            }
        },
        *getViewAuthority({payload},{call,put,select}){
            // console.log(payload);
            if(payload){
               let result = yield call(getViewAuthority,payload.user_id)

                if(result.code === 1){
                       //用户视图筛选
                    let list:ViewData[]=[]
          
                    menuList.forEach(item=>{
                        let children=item.children.filter(val=>{
                        return result.data.findIndex((v: { view_id: string; })=>v.view_id===val.meta) != -1
                        })
                        list.push({...item,children})
                    })
                    
                    list=list.filter(item=>item.children.length!==0)
                    yield put({
                        type:'save',
                        payload:{userMenu:list}
                    })
                }
            }
            
        }
    },
    reducers:{
        save(state, action) {
            return {
              ...state,
              ...action.payload,
            };
            
          }
    },
    subscriptions: {
        setup({ dispatch, history }) {
          return history.listen(({ pathname }) => {
                let token = getCookie('token');
                if(token){
                    if(pathname === '/login'){
                        history.replace('/main');
                    }
                    if(!isUserInfo){
                        dispatch({
                            type:'getUserInfo'
                        })
                    }
                }else{
                    if(pathname !== '/login'){
                        history.replace(`/login?redirect=${encodeURIComponent(pathname)}`)
                    }
                }
                if (!isUserInfo && token) {
                    dispatch({
                        type: 'getUserInfo'
                    })
                }
          });
        }
    }
}

export default UserModel